package com.hake.manager.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hake.core.exception.AppException;
import com.hake.core.jedis.JedisClientSingle;
import com.hake.core.jedis.RedisKey;
import com.hake.core.support.service.BaseService;
import com.hake.core.util.ProtoStuffSerializerUtil;
import com.hake.manager.system.entity.Menu;
import com.hake.manager.system.mapper.MenuMapper;
import com.hake.manager.system.service.MenuService;

@Service
public class MenuServiceImpl extends BaseService<Menu> implements MenuService {

	@Autowired
	private MenuMapper mapper ;
	
	@Autowired
	private JedisClientSingle jedisClientSingle ;
	
	@Override
	public int insertSelective(Menu entity) throws AppException {
		if(null != entity && entity.getDefaultSelect()) {
			this.mapper.updateDefaultSelect(); 
		}
		
		return super.insertSelective(entity);
	}
	
	@Override
	public int updateByPrimaryKeySelective(Menu entity)
			throws AppException {
		
		if(null != entity && entity.getDefaultSelect()) {
			this.mapper.updateDefaultSelect(); 
		}
		
		return super.updateByPrimaryKeySelective(entity);
	}
	
	@Override
	public List<Menu> selectMenuByCategory() {
		return this.mapper.selectMenuByCategory();
	}

	@Override
	public List<Menu> selectMenuByParentId(Integer parentId) {
		return this.mapper.selectMenuByParentId(parentId);
	}

	@Override
	public Menu selectMenuByLocation(String location) {
		return this.mapper.selectMenuByLocation(location);
	}

	@Override
	public List<Menu> treegrid() {
		List<Menu> menus = new ArrayList<Menu>() ;
		List<Menu> catalogList = this.selectMenuByCategory() ;
		for (Menu menu : catalogList) {
			menus.add(this.recursiveMenu(menu)) ;
		}
		return menus;
	}

	/**
	 * 递归
	 * @param entity
	 * @return
	 */
	private Menu recursiveMenu(Menu entity) {
		entity.setIconCls("icon-standard-application-view-list");
		List<Menu> childs = this.mapper.selectMenuByParentId(entity.getId());
		if(null != childs && !childs.isEmpty()) {
			List<Menu> children = new ArrayList<Menu>() ;
			
			for (Menu r : childs) {
				if(null != r) {
					children.add(recursiveMenu(r)); 
				}
			}
			entity.setChildren(children);
		}
		return entity ;
	}
	
	
	@Override
	public List<Menu> selectMenuByCategoryForCache() {
		byte[] key = RedisKey.toBytes("selectMenuByCategoryForCache") ;
		
		byte[] bs = jedisClientSingle.get(key) ;
		if(null == bs) {
			List<Menu> selectMenuByCategory = this.selectMenuByCategory() ;
			jedisClientSingle.set(key, ProtoStuffSerializerUtil.serializeList(selectMenuByCategory)) ;
			
			return selectMenuByCategory ;
		} else {
			return ProtoStuffSerializerUtil.deserializeList(bs, Menu.class) ;
		}
	}
	
	@Override
	public List<Menu> selectMenuByParentIdForCache(Integer parentId) {
		byte[] key = RedisKey.toBytes("selectMenuByParentIdForCache:" + parentId) ;
		
		byte[] bs = jedisClientSingle.get(key) ;
		if(null == bs) {
			List<Menu> selectMenuByParentId = this.selectMenuByParentId(parentId) ;
			jedisClientSingle.set(key, ProtoStuffSerializerUtil.serializeList(selectMenuByParentId)) ;
			
			return selectMenuByParentId ;
		} else {
			return ProtoStuffSerializerUtil.deserializeList(bs, Menu.class) ;
		}
	}

	@Override
	public Menu selectMenuByLocationForCache(String location) {
		byte[] key = RedisKey.toBytes(location) ;
		
		byte[] bs = jedisClientSingle.get(key) ;
		if(null == bs) {
			Menu selectMenuByLocation = this.selectMenuByLocation(location) ;
			jedisClientSingle.set(key, ProtoStuffSerializerUtil.serialize(selectMenuByLocation)) ;
			
			return selectMenuByLocation ;
		} else {
			return ProtoStuffSerializerUtil.deserialize(bs, Menu.class) ;
		}
	}

	@Override
	public List<Menu> treegridForCache() {
		byte[] key = RedisKey.toBytes("menu:treegrid") ;
		
		byte[] bs = jedisClientSingle.get(key) ;
		if(null == bs) {
			List<Menu> treegrid = this.treegrid() ;
			jedisClientSingle.set(key, ProtoStuffSerializerUtil.serializeList(treegrid)) ;
			
			return treegrid ;
		} else {
			return ProtoStuffSerializerUtil.deserializeList(bs, Menu.class) ;
		}
	}

	
}
